package org.example.study.sc.controller;

import org.example.common.result.R;
import org.example.study.sc.constant.SecKillConstant;
import org.example.study.sc.dto.SecKillMsg;
import org.example.study.sc.rabbitmq.OrderSender;
import org.example.study.sc.service.GoodsSekKilService;
import org.example.study.sc.service.SecKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: rabbitmq-study
 * @description:
 * @Version V1.0.0
 * @author: dazhi
 * @create: 2021-02-02 11:05
 **/
@RestController
public class SecKillController {

    @Autowired
    private OrderSender orderSender;
    @Autowired
    private GoodsSekKilService goodsSekKilService;
    @Autowired
    private SecKillService secKillService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @GetMapping(value = "/initStock/{secKillId}/{stock}")
    public R<String> initSku(@PathVariable("secKillId") Long secKillId, @PathVariable("stock") Integer stock) {
        //初始化库存数量
        stringRedisTemplate.opsForValue().set(SecKillConstant.secKillRedisKey + secKillId, stock.toString());
        return R.data("库存初始化成功");
    }


    @GetMapping("secKill/{secKillId}/{memberId}")
    public R<Boolean> secKill(@PathVariable("secKillId") Long secKillId, @PathVariable("memberId") Long memberId) {
        SecKillMsg secKillMsg = new SecKillMsg(secKillId, memberId);
        orderSender.sendSecKillMsg(secKillMsg);
        return R.data(true);
    }

}
